<template>
  <div class="AddForm" v-loading="loadingFlag">
    <FilterTable
      ref="table"
      :columns="tablecolumns"
      :dataSource="dataSource"
      :isSelectColumn="false"
      :isConfigPageSize="50"
      :datakey="'records'"
      columnsRef="userManage"
      :onColumnsChange="(columns) => (this.tablecolumns = columns)"
    >
      <template slot="controls">
        <el-button size="small" type="primary" @click="addhandle">新增</el-button>
      </template>

    </FilterTable>
    <BaseDialog title="物料组查询查询" :visible.sync="isShowSearchCustomer" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SearchGroupInfo :customerNo="kehuxuanze" @close="isShowSearchCustomer = false" @addCustomerCode="confirmSearchCustomer" />
    </BaseDialog>
    <div class="dialogBottmView" v-if="showType != 'show'">
      <el-button @click="closeDialog('refresh')">关 闭</el-button>
    </div>
  </div>
</template>

<script>
/* eslint-disable eqeqeq */
import { getPartGroupCodeInfoPage,deleteGroupCodeOnly} from '@/api/partsmanage/partTypeManage/query';
import SearchGroupInfo from './SearchGroupInfo';
export default {
  name: 'addForm',
  components: {
    SearchGroupInfo
  },
  props: {
    initData: {},
    showType: {},
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      isShow: false,
      type: '',
      dataSource: [],
      isSubmitOk: false,
      loadingFlag: false,
      tablecolumns: this.createTableColumns(),
      isShowSearchCustomer: false,
      kehuxuanze: ''
    };
  },
  created() {
    this.findBuyItems(this.rowData);
  },
  methods: {
    async handleInputChange(val, key) {
      //这段代码一定要有，清缓存，避免上一次输入影响结果
      if (!val) {
        this.formList.find(x => x.fieldName === 'code').initialValue = '';
        this.formList.find(x => x.fieldName === 'typeName').initialValue = '';
        return;
      }
      const tempObj = {};
      if (key === 'code') {
        tempObj.code = val;
      }
      if (key === 'typeName') {
        tempObj.typeName = val;
      }
      const res = await getPartTypeBasePage(tempObj);
      //异步获取是否存在备件分类
      if (res && res.data) {
        this.formList.find(x => x.fieldName === 'code').initialValue = '';
        this.formList.find(x => x.fieldName === 'typeName').initialValue = '';
        return this.$message.warning('备件分类代码或名称已存在，请重新输入！');
      }
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span> {props.row.index + 1} < /span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          fixed: 'left',
          render: (props) => {
            return (
              <div>
              <el-button size = "mini" type = "text" onClick = {() => this.edit(props.row,this.rowData.id)}>
            删除
            </el-button>
            </div>
          );
          }
        },
        {
          title: '物料组代码',
          dataIndex: 'GROUP_CODE',
        },
        {
          title: '物料组名称',
          dataIndex: 'GROUP_NAME',
        }
      ]
    },
    // 保存-编辑
    // 返回
    closeDialog() {
      this.$emit('close');
    },
    addhandle(){
      this.kehuxuanze=this.rowData.id;
      this.isShowSearchCustomer = true;
    },
    handleNumber(val, key) {
      this.$nextTick(() => this.formList.find(x => x.fieldName === key).initialValue = val.replace(/[^0-9]/g, ''));
    },
    edit(data, config){
      deleteGroupCodeOnly({id: config,groupCode:data.GROUP_CODE}).then(res => {
        if (res) {
          this.$message({
            type: 'info',
            message: '删除成功'
          });
          this.dataSource = res.data;
        } else {
          this.$message({
            type: 'info',
            message: '暂无记录可删除'
          });
          this.dataSource = [];
        }
      })
    },
    findBuyItems(data) {
      console.log("请求返回成功");
      getPartGroupCodeInfoPage({id: this.rowData.id}).then(res => {
        if (res) {
          this.dataSource = res.data;
        } else {
          this.dataSource = [];
        }
      })
    }
  }
};
</script>

<style lang="less" scoped>
.clearfix::after {
  content: "";
  height: 0;
  visibility: hidden;
  clear: both;
  display: block;
}
.clearfix {
  zoom: 1;
}
.AddForm {
  height: calc(100vh - 110px);
  overflow: scroll;
  padding-bottom: 60px;
}
</style>
